package invest.data.getdata;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import invest.data.Data;
import invest.pojo.XueqiuCookie;
import invest.pojo.csv.StockHistoryCsv;
import invest.pojo.datapojo.KlineHistory;
import invest.pojo.datapojo.StockHistory;
import invest.utils.HttpRequest;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import static invest.utils.CodeConverterUtil.convertCode;
import static invest.utils.CsvUtil.getCsvDataColumnHead;

/**
 * @author zhichao.ni
 * 2021/2/5
 **/
//获取股票历史信息
public class StockHistoryData implements Data<List<KlineHistory>> {


    String code="";

    String endTime="";

    Integer count = 10;
    private final String pre = "https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=";
//    private final String after = "";
      String p1 = "&begin=";
    String middle = "&period=day&type=before&count=-";
    String after = "&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance";

    public StockHistoryData(String code) {
        if (code.startsWith("6") ) {
            this.code = "SH" + code;
        } else {
            this.code = "SZ" + code;
        }

    }

    public StockHistoryData(String code, Integer count) {
        if (code.startsWith("6") ) {
            this.code = "SH" + code;
        } else {
            this.code = "SZ" + code;
        }
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyyMMdd");
//        this.p1 = sdfs.format(p1);
//        this.after = sdfs.format(endTime);
        this.count = count;
    }

    //    private String url = "https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=SZ128033&begin=1617021934480&period=day&type=before&count=-100000&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance";



    @Override
    public List<KlineHistory> getData() throws IOException {
        String cookie = XueqiuCookie.getXueqiuCookie();
        Long timeMillis = System.currentTimeMillis(); // 当前时间戳
        String url = pre + code + p1 + String.valueOf(timeMillis) +middle+String.valueOf(count)+ after;
        String result = HttpRequest.sendGet(url,  cookie);
        JSONObject jb = JSONObject.parseObject(result);
        JSONObject data = jb.getJSONObject("data");
        JSONArray item = data.getJSONArray("item");
        List<KlineHistory> hkStockDayKlines = new ArrayList<>();
        if (item == null) {
            return null;
        }
        for (int i = 0; i <item.size() ; i++) {
            JSONArray jsonArray=item.getJSONArray(i);
            KlineHistory klineHistory = new KlineHistory();
            klineHistory.setDate(new Date((Long) jsonArray.get(0)));
            klineHistory.setVolume((Integer) jsonArray.get(1));
            klineHistory.setOpen(new Double(((BigDecimal) jsonArray.get(2)).doubleValue()));
            klineHistory.setHigh(new Double(((BigDecimal) jsonArray.get(3)).doubleValue()));
            klineHistory.setLow(new Double(((BigDecimal) jsonArray.get(4)).doubleValue()));
            klineHistory.setClose(new Double(((BigDecimal) jsonArray.get(5)).doubleValue()));
            klineHistory.setChg(new Double(((BigDecimal) jsonArray.get(6)).doubleValue()));
            klineHistory.setPercent(new Double(((BigDecimal) jsonArray.get(7)).doubleValue()));
            if (jsonArray.get(8)== null) {
//                klineHistory.setTurnoverrate(new Double(((BigDecimal) jsonArray.get(8)).doubleValue()));
                break;
            } else {
                klineHistory.setTurnoverrate(new Double(((BigDecimal) jsonArray.get(8)).doubleValue()));
            }

            klineHistory.setAmount(new Double(((BigDecimal) jsonArray.get(9)).doubleValue()));
            hkStockDayKlines.add(klineHistory);
        }
        Collections.reverse(hkStockDayKlines);
        return hkStockDayKlines;
    }



    public static void main(String[] args) throws IOException {
        StockHistoryData stockHistory = new StockHistoryData("600009",1000);
        List<KlineHistory> stockHistories = stockHistory.getData();
        System.out.println(1);
    }
}
